﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using InHD.model;
using System.Windows.Input;
using InHD.commands;
using System.Collections.ObjectModel;
using InHD.dataaccess;
using System.Data;
using InHD.utilities;
using System.Windows;
using InHD.Properties;

namespace InHD.viewmodels
{
    class AccPayableFormViewModel : WorkspaceViewModel
    {
        #region Constructor
        public AccPayableFormViewModel(Transaction transaction, CompanyInfo companyInfo)
        {
            _transaction = transaction;
            _companyInfo = companyInfo;
            _packageRepo = new PackageRepository();            

            this._pacs = _packageRepo.GetPackgesByTransaction(_transaction.TransactionUNK);
        }
        #endregion

        #region Value Property
        private Transaction _transaction;
        public string commandExcute = string.Empty;
        private CompanyInfo _companyInfo;

        private PackageRepository _packageRepo;
        #endregion

        private double exTax, incTax, tax;
        bool _isWholesale;
        private List<Package> _pacs;

        #region Presentation Property

        public Transaction Transaction
        {
            get
            {
                return _transaction;
            }
        }

        public bool IsIncludeTax
        {
            get { return string.IsNullOrEmpty(this._transaction.TaxUNK) ? false : true; }
        }

        public string TotalPriceExTax
        {
            get
            {
                return Resources.Ex_Tax + ((long)(this.Transaction.TotalSaleAmount / (1 + _transaction.TaxPercentage))).ToString();
            }
        }

        public string TotalPriceTax
        {
            get
            {
                return !IsIncludeTax ? Resources.Tax_Title + " 0 %" : Resources.Tax_Title + ((long)(this.Transaction.TaxPercentage * 100)).ToString() + " %";
            }
        }

        public string TotalPriceIncTax
        {
            get
            {
                return Resources.Inc_Tax + ((long)this.Transaction.TotalSaleAmount).ToString();
            }
        }

        public string CustomerName
        {
            get
            {
                return this._transaction.CustomerFirstName;
            }
        }

        public bool IsWholesale
        {
            get { return this._isWholesale; }
        }


        public List<Package> PackagesCollection
        {
            get
            {
                return this._pacs;
            }
            set
            {
                if (_pacs != value)
                {
                    this._pacs = value;
                    OnPropertyChanged("PackagesCollection");
                }
            }
        }

        public string supplierCode
        {
            get;
            set;
        }
        public string supplierName
        {
            get;
            set;
        }

        public string Tax
        {
            get
            {
                return this._transaction.TaxName;
            }
        }

        #endregion

        #region Command
        private RelayCommand _closeCommand;
        public ICommand CancelCommand
        {
            get
            {
                if (_closeCommand == null)
                {
                    _closeCommand = new RelayCommand(
                        (param) => this.Close());
                }
                return _closeCommand;
            }
        }

        private RelayCommand _getAccpayableCommand;
        public ICommand GetAccpayableCommand
        {
            get
            {
                if (_getAccpayableCommand == null)
                {
                    _getAccpayableCommand = new RelayCommand(
                        (param) => this.GetAccpayable());
                }
                return _getAccpayableCommand;
            }
        }

        #endregion

        #region Command Method
        void Close()
        {
            commandExcute = "CloseTransactionDetail";
            OnRequestClose();
        }

        void GetAccpayable()
        {
            TransactionRepository _transRepo = new TransactionRepository();
            _transRepo.DeleteTransaction(Transaction.TransactionUNK);

            commandExcute = "GetAccpayable";
            OnRequestClose();
        }

        private RegisterPrintingRepository _regRepo = new RegisterPrintingRepository();
        private InvoicePrinter _invoicePrinter;

        #endregion

        
    }
}
